package com.baimifan.renovation.publicfrag;


import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.FindAdapter;
import com.baimifan.renovation.manager.FindManager;
import com.baimifan.renovation.tools.MyDecoration;

import java.lang.ref.WeakReference;

/**
 * 装修管理类目下-公用fragment
 */
public class DecorationFragment extends Fragment {
    private Float w, h, widthPixels, heightPixels;
    private int typeId;
    private RecyclerView recyclerView;
    private MyHandler handler;
    private FindManager findManager;
    private FindAdapter findAdapter;

    public static DecorationFragment newInstance (int typeId) {
        DecorationFragment decorationFragment = new DecorationFragment();
        Bundle args = new Bundle();
        args.putInt("typeId", typeId);
        decorationFragment.setArguments(args);
        return decorationFragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            if (getArguments() != null) {
                typeId = getArguments().getInt("typeId", 0);
                handler = new MyHandler(this);
                findManager = new FindManager(handler);
                findManager.finddata(typeId + "");
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_decoration, container, false);

        init(view);
        getDisplay();
        setLayoutParams();
        return view;
    }

    private void init (View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.decora_recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new MyDecoration(getActivity(), MyDecoration.VERTICAL_LIST));
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setLayoutParams() {
        LinearLayout.LayoutParams params_re = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params_re.setMargins(0, 13, 0, 0);
        recyclerView.setLayoutParams(params_re);
    }

    private class  MyHandler extends Handler {
        WeakReference<DecorationFragment> mdecora;

        public MyHandler (DecorationFragment decorationFragment) {
            mdecora = new WeakReference<DecorationFragment>(decorationFragment);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DecorationFragment decora = mdecora.get();
            switch (msg.what) {
                case 7:
                    findAdapter = new FindAdapter(findManager.getFind(), w, h);
                    recyclerView.setAdapter(findAdapter);
                    break;
            }
        }
    }
}
